package org.sport.dbImplement;

import org.sport.controller.MainController;
import org.sport.dbInterface.UserOperation;
import org.sport.model.entityModel.UsertableEntity;
import org.sport.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.sql.Timestamp;
import java.util.List;

/**
 * Created by Allen on 2017/9/11.
 */

@Service
public class UserOpeImp implements UserOperation {

    @Autowired
    private UserRepository userRepository2;

    @Override
    public boolean isUserByPhone(UsertableEntity usertableEntity) {

        if(userRepository2.findOne(usertableEntity.getId()) == null)
            return false;
        else
            return true;
    }

    @Override
    public UsertableEntity findUserByPhone(UsertableEntity usertableEntity) {

        if(userRepository2 == null)
        {
            System.out.println("***********");
        }

        String phone = usertableEntity.getPhoneNumber();
        return userRepository2.finduserByPhoneNumber(phone) ;

    }

    @Override
    public List<UsertableEntity> findAllUsers() {

        List<UsertableEntity> userList = userRepository2.findAll();
        return userList;
    }


    /*
    * 增加一个usertable实体
    * */
    @Override
    public boolean                     addUserEntity(UsertableEntity usertableEntity)
    {
        if(userRepository2.saveAndFlush(usertableEntity) != null)
            return true;
        else
            return false;
    }

    @Override
    public boolean delUserEntity(UsertableEntity usertableEntity) {

        if (usertableEntity.getPhoneNumber() != null) {

            userRepository2.delete(usertableEntity.getId());
            String phone = usertableEntity.getPhoneNumber();
            UsertableEntity usertableEntity2 = userRepository2.finduserByPhoneNumber(phone);
            if (usertableEntity2 == null) {
                return true;
            } else {
                return false;
            }
        }
        else
        {
            System.out.printf("传入的user为null");
            return false;
        }
    }


    /*
    * 更新对应id的UserEntity
    * */
    @Override
    public boolean updateUserEntity(UsertableEntity usertableEntity) {


        String name                 = usertableEntity.getName();
        String password             = usertableEntity.getPassword();
        String email                = usertableEntity.getEmail();
        String phoneNumber          = usertableEntity.getPhoneNumber();
        String brief                = usertableEntity.getBrief();
        String headIcon             = usertableEntity.getHeadIcon();
        String idCardNumber         = usertableEntity.getIdCardNumber();
        Timestamp birthday          = usertableEntity.getBirthday();
        Integer awardNumber             = usertableEntity.getAwardNumber();
        Integer punchNumber             = usertableEntity.getPunchNumber();
        Integer punchToday              = usertableEntity.getPunchToday();
        Integer id                      = usertableEntity.getId();
        userRepository2.updateUser(phoneNumber,name,password,email,brief,headIcon,idCardNumber,birthday,awardNumber,punchNumber,punchToday,id);
        userRepository2.flush();
        return true;
    }


    public UsertableEntity findUserById(Integer id)
    {
        return userRepository2.findOne(id);
    }
}
